
/* GCSx
** TILESETEDIT.H
**
** Tileset support
** Expands basic TileSet to include editor functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_TILESETEDIT_H_
#define __GCSx_TILESETEDIT_H_

class TileSetEdit : public TileSet, virtual public SaveLoad {
protected:
    // Has the tileset been modified from initial state (from empty if new)
    int headerModified;
    int contentModified;
    int disassociated;

    // Our node on the world browser
    TreeView* browserNode;

    // Set us as modified
    // Setting content modified also loads from cache if needed
    // Call these before actually making any modifications
    // (if header changes affect content, call both modifieds first)
    void setHeaderModified();
    void setContentModified();

public:
    // Id of 0 = we're going to load (or create/import) immediately
    // Any other id = we start out modified
    // World and ID may be NULL/0 if in process of creating
    TileSetEdit(class WorldEdit* myWorld, int newIsFont, int myId = 0); // Starts with default settings
    virtual ~TileSetEdit();
    
    // Intended only for use after creating/importing- not in general usage
    void setInfo(class WorldEdit* myWorld, int newId);
    
    // Tells us to disassociate ourselves from anything, we've been "deleted"
    // or that we've been "readded" again
    // FileException means couldn't decache, and nothing was done
    void disassociate() throw_File;
    // Node is world browser node that we readd to
    void reassociate(TreeView* node);

    class WorldEdit* getWorldEdit() { return dynamic_cast<WorldEdit*>(world); }
    
    // Doesn't remove from world or erase name
    // Sets up to have a given number of blank tiles
    /* unused; doesn't affect collision maps
    void clear(int w, int h, int count = 0); // Must LOCK first
    */
    
    // Change properties; handles undo
    // setSize assumes you're passing a valid combination of sizes that has been verified
    // surfaces are used by undo/redo calling us; standard use can ignore
    void setSize(int newWidth, int newHeight, int newCount, int newCollisionCount, Window* srcWin = NULL, Window* exWin = NULL, SDL_Surface** undo1 = NULL, Uint32** undo2 = NULL, Uint32** undo3 = NULL) throw_Undo; // Must LOCK first
    void setName(const std::string& newName, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    // (unused) void setIsFont(int newIsFont); // Must LOCK first
    void setTilesPerLine(int newTilesPerLine, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    void setDefaultTransparent(int newDefaultTransparent, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo; // Must LOCK first
    
    // For viewing data in editor
    // Tile 0 displays nothing
    // Tile above max tiles displays debugging info (solid tile with tile number)
    void blitTile(int tileNum, SDL_Surface* surface, int dX, int dY) const; // Must LOCK first
    // Blits data with options- not optimized, intended for editor
    // alpha and color bitdepth taken from gcsx_imgselect.h
    void blitTileFx(int tileNum, SDL_Surface* surface, int dX, int dY, int alpha, int mirror, int flip, int rotate90, int color) const; // Must LOCK first
    void blitCollFx(int collNum, SDL_Surface* surface, int dX, int dY, int dim, int mirror, int flip, int rotate90, Uint32 color) const; // Must LOCK first
    
    // Copies a tile/collision map, assumes surface is 32bpp/same width/height (or larger) as tiles
    // May not respect surface clipping (assumes drawing to a scratch area)
    // exWin is window to exempt from resulting update event; does NOT handle undo
    // collision data uses white/100% as on and black/100% as off; save treats non-white as black
    // (tile is 1+; collision is 1+)
    void loadTile(int tileNum, SDL_Surface* surface); // Must LOCK first
    void loadColl(int collNum, SDL_Surface* surface); // Must LOCK first
    void saveTile(int tileNum, SDL_Surface* surface, Window* exWin); // Must LOCK first
    void saveColl(int collNum, SDL_Surface* surface, Window* exWin); // Must LOCK first
    // Font sets only; width must 0 or more
    void setGlyphWidth(int tileNum, int newWidth, Window* exWin); // Must LOCK first
    
    // Lets editor directly view tile data, for certain optimized uses
    const Uint8* viewTileData(int tile) const; // Must LOCK first
    const Uint32* viewCollData(int coll) const; // Must LOCK first
    // Pitch is number of Uint8s per line, 4x pixels plus possibly some padding
    int viewTilePitch() const; // Must LOCK first

    // Lets editor directly EDIT tile data; also uses viewTilePitch()
    // Must call editTileDone when complete; does NOT handle undo
    Uint8* editTileData(int tile); // Must LOCK first
    Uint32* editCollData(int coll); // Must LOCK first
    void editTileDone(int tile, Window* exWin = NULL); // Must LOCK first
    void editCollDone(int coll, Window* exWin = NULL); // Must LOCK first

    // Returns true if OK pressed
    // If newTileSet is true, and count is 0, count will reset to default
    // Locks and unlocks tileset
    int propertiesDialog(int newTileSet = 0, Window* srcWin = NULL, Window* exWin = NULL);
    
    // Browse window (editor)
    void openBrowseWindow();
    
    // Tile/collision paint edit window (editor)
    void openTilePaintWindow(int tile = 1);
    void openCollPaintWindow(int coll = 1);

    // Tells tileset to add itself to the world browser node
    void addToBrowser(TreeView* node, int makeCurrent = 1);
    void dropBrowser(); // Called if tileset gets dropped from browser

    // Treeview event handling
    int treeviewEvent(int code, int command, int check);
    static int treeviewEventWrap(void* ptr, int code, int command, int check);
    
    // Takes desired tile width, height, count
    // Determines surface width (pitch), height, num tiles per line
    // Returns the number of tiles that fit onto the surface, which may
    // be less than the requested count, but won't be more
    static int calculateTileSurfaceSizing(int w, int h, int count, int& sWidth, int& sHeight, int& perLine);
    
    // File access
    int isHeaderModified() const;
    int isContentModified() const;
    Uint32 saveHeader(FileWrite* file) throw_File;
    void saveContent(FileWrite* file) throw_File;
    void saveSuccess();
    void cachedContent(FileRead* file, int oldData);
};

#endif

